home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
1035
/
1035.xpi
/
chrome
/
1clickweather.jar
/
content
/
1clickweather
/
js
/
config
/
configutils.js
< prev
next >
Wrap
Text File
|
2008-10-05
|
15KB
|
320 lines
// ⌐ 2005 The Weather Channel Interactive, Inc. All Rights Reserved.
function ConfigUtils() {
this.separator = ","; // use comma as 000's separator
this.decpoint = "."; // use period as decimal point
this.percent = "%";
this.currency = "$"; // use dollar sign for currency
}
ConfigUtils.prototype = {
separator : null,
decpoint : null,
percent : null,
currency : null,
getUrl : function(appCfg, userCfg, dataKey, forcedLocation){
try{
var dataUrl = appCfg.getDataByName(dataKey).getDataURL().getBase();
if(forcedLocation){
dataUrl += forcedLocation;
}else if(dataKey != "Search"){ // search doesn't know what locid to use
dataUrl += userCfg.getAllProfiles().getDefaultProfile().getSetup().getLocation().getLocID();
}
dataUrl += '?';
var urlParams = appCfg.getDataByName(dataKey).getDataURL().getParams();
var max = this.getHashSize(urlParams);
var paramCounter = 0;
for(var i in urlParams){
var name = urlParams[i].getName();
var val = "";
switch(name){
// right now, if it's not standard units, force metric
case "unit":
val = userCfg.getAllProfiles().getDefaultProfile().getSetup().getUnits().getName();
if(val != 's'){
val = 'm';
}
break;
// some kinds of params only need a '*' to turn them on/off
case "cc":
case "swa":
val = '*';
break;
// we need to turn the extended location on to get country/state
case "eloc":
val = 'st';
break;
// the number of days has to include one extra day to account for the first day (day0) is not part
// of the max number of days to show in the extended forecast.
case "dayf":
val = userCfg.getAllProfiles().getDefaultProfile().getExtForecast().getExtDetails().getDays();
val++;
// now, if the user wants the first day's forecast to switch to tomorrow after 3pm, we need
// an extra day of forecast data
if(userCfg.getAllProfiles().getDefaultProfile().getForecast().getForecastSwitch() == "tomorrow"){
val++;
}
if(val > MAXFCSTDAYS){
val = MAXFCSTDAYS;
}else if(val < 1){
val = 1;
}
break;
}
// as long as we are not the first or last param element, put a '&' between params
if((paramCounter != 0) && (i != (max - 1))){
dataUrl += '&';
}
dataUrl += name + '=' + val;
paramCounter++;
}
return(dataUrl);
}catch(e){
alert('cfg err: ' + e);
}
},
getAppDataURL : function(myAppConfig, myDataElementName, myLocID, myParamValuesArray) {
try {
var urlBase = myAppConfig.getDataByName(myDataElementName).getDataURL().getBase();
var urlParams = myAppConfig.getDataByName(myDataElementName).getDataURL().getParams();
var toRetURL = urlBase;
toRetURL += myLocID;
var i = 0;
var max = this.getHashSize(urlParams);
for(var nextParamKey in urlParams) {
if(i == 0) {
toRetURL += "?";
}
var nextParam = urlParams[nextParamKey];
toRetURL += nextParam.getName();
toRetURL += "=";
// CHECK if the param name is "unit"
var paramVal = myParamValuesArray[nextParam.getName()];
if(nextParamKey == "unit") {
if(paramVal != "s") {
paramVal = "m";
}
}
toRetURL += paramVal;
if(i != max - 1) {
toRetURL += "&";
}
i++;
}
return toRetURL;
} catch(e) {
return "";
}
},
getHashSize : function(myHash) {
var i = 0;
for(var nk in myHash) {
i++;
}
return i;
},
convertData : function(myUserConfig, myDataElementName, xmlDoc) {
var myUnits = myUserConfig.getAllProfiles().getDefaultProfile().getSetup().getUnits().getName();
if(myUnits == "m" || myUnits == "s" || myUnits == "mk") {
// nothing to convert!
} else if(myUnits == "ms") {
// ASSUME THAT DATA CAME IN IN METRIC VALUES!!!!!!!!!
// WE NEED TO COVERT SPEED FROM KM/H to M/S
if(myDataElementName == "Obs") {
var newUS = xmlDoc.createElement("us");
newUS.appendChild(xmlDoc.createTextNode("m/s"));
xmlDoc.getElementsByTagName("head")[0].removeChild(xmlDoc.getElementsByTagName("head")[0].getElementsByTagName("us")[0]);
xmlDoc.getElementsByTagName("head")[0].appendChild(newUS);
var windSpeedVal = xmlDoc.getElementsByTagName("s")[0].firstChild.nodeValue;
var newWindSpeedVal = windSpeedVal * 0.277778;
newWindSpeedVal = this.formatNumber(newWindSpeedVal, "##0");
var newWindSpeedElement = xmlDoc.createElement("s");
newWindSpeedElement.appendChild(xmlDoc.createTextNode(newWindSpeedVal));
xmlDoc.getElementsByTagName("cc")[0].getElementsByTagName("wind")[0].removeChild(xmlDoc.getElementsByTagName("cc")[0].getElementsByTagName("wind")[0].getElementsByTagName("s")[0]);
xmlDoc.getElementsByTagName("cc")[0].getElementsByTagName("wind")[0].appendChild(newWindSpeedElement);
} else if(myDataElementName == "Fcst" || myDataElementName == "ExtFcst") {
var newUS = xmlDoc.createElement("us");
newUS.appendChild(xmlDoc.createTextNode("m/s"));
xmlDoc.getElementsByTagName("head")[0].removeChild(xmlDoc.getElementsByTagName("head")[0].getElementsByTagName("us")[0]);
xmlDoc.getElementsByTagName("head")[0].appendChild(newUS);
var allDays = xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day");
var i =0;
for(i=0;i<allDays.length;i++) {
var newDay = allDays[i];
var newDayParts = newDay.getElementsByTagName("part");
var j = 0;
for(j=0;j<newDayParts.length;j++) {
var newDayPart = newDayParts[j];
var windSpeedVal = newDayPart.getElementsByTagName("wind")[0].getElementsByTagName("s")[0].firstChild.nodeValue;
var newWindSpeedVal = windSpeedVal * 0.277778;
newWindSpeedVal = this.formatNumber(newWindSpeedVal, "##0");
var newWindSpeedElement = xmlDoc.createElement("s");
newWindSpeedElement.appendChild(xmlDoc.createTextNode(newWindSpeedVal));
xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day")[i].getElementsByTagName("part")[j].getElementsByTagName("wind")[0].removeChild(xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day")[i].getElementsByTagName("part")[j].getElementsByTagName("wind")[0].getElementsByTagName("s")[0]);
xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day")[i].getElementsByTagName("part")[j].getElementsByTagName("wind")[0].appendChild(newWindSpeedElement);
}
}
} else {
// dont know what kind of data this is!
}
} else if(myUnits == "uk"){
// ASSUME THAT DATA CAME IN IN METRIC VALUES!!!!!!!!!
// WE NEED TO CONVERT SPEED KM/H to MPH
if(myDataElementName == "Obs") {
var newUS = xmlDoc.createElement("us");
newUS.appendChild(xmlDoc.createTextNode("mph"));
xmlDoc.getElementsByTagName("head")[0].removeChild(xmlDoc.getElementsByTagName("head")[0].getElementsByTagName("us")[0]);
xmlDoc.getElementsByTagName("head")[0].appendChild(newUS);
var windSpeedVal = xmlDoc.getElementsByTagName("s")[0].firstChild.nodeValue;
var newWindSpeedVal = windSpeedVal * 0.621371192;
newWindSpeedVal = this.formatNumber(newWindSpeedVal, "##0");
var newWindSpeedElement = xmlDoc.createElement("s");
newWindSpeedElement.appendChild(xmlDoc.createTextNode(newWindSpeedVal));
xmlDoc.getElementsByTagName("cc")[0].getElementsByTagName("wind")[0].removeChild(xmlDoc.getElementsByTagName("cc")[0].getElementsByTagName("wind")[0].getElementsByTagName("s")[0]);
xmlDoc.getElementsByTagName("cc")[0].getElementsByTagName("wind")[0].appendChild(newWindSpeedElement);
} else if(myDataElementName == "Fcst" || myDataElementName == "ExtFcst") {
var newUS = xmlDoc.createElement("us");
newUS.appendChild(xmlDoc.createTextNode("mph"));
xmlDoc.getElementsByTagName("head")[0].removeChild(xmlDoc.getElementsByTagName("head")[0].getElementsByTagName("us")[0]);
xmlDoc.getElementsByTagName("head")[0].appendChild(newUS);
var allDays = xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day");
var i =0;
for(i=0;i<allDays.length;i++) {
var newDay = allDays[i];
var newDayParts = newDay.getElementsByTagName("part");
var j = 0;
for(j=0;j<newDayParts.length;j++) {
var newDayPart = newDayParts[j];
var windSpeedVal = newDayPart.getElementsByTagName("wind")[0].getElementsByTagName("s")[0].firstChild.nodeValue;
var newWindSpeedVal = windSpeedVal * 0.621371192;
newWindSpeedVal = this.formatNumber(newWindSpeedVal, "##0");
var newWindSpeedElement = xmlDoc.createElement("s");
newWindSpeedElement.appendChild(xmlDoc.createTextNode(newWindSpeedVal));
xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day")[i].getElementsByTagName("part")[j].getElementsByTagName("wind")[0].removeChild(xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day")[i].getElementsByTagName("part")[j].getElementsByTagName("wind")[0].getElementsByTagName("s")[0]);
xmlDoc.getElementsByTagName("dayf")[0].getElementsByTagName("day")[i].getElementsByTagName("part")[j].getElementsByTagName("wind")[0].appendChild(newWindSpeedElement);
}
}
} else {
// dont know what kind of data this is!
}
} else {
// dont know what to do....
// this is bigger problem
}
return xmlDoc;
},
// Original JavaScript code of this function by Duncan Crombie: dcrombie@chirp.com.au
formatNumber : function(number, format) { // use: formatNumber(number, "format")
if (number - 0 != number) return null; // if number is NaN return null
var useSeparator = format.indexOf(this.separator) != -1; // use separators in number
var usePercent = format.indexOf(this.percent) != -1; // convert output to percentage
var useCurrency = format.indexOf(this.currency) != -1; // use currency format
var isNegative = (number < 0);
number = Math.abs (number);
if (usePercent) number *= 100;
format = this.strip(format, this.separator + this.percent + this.currency); // remove key characters
number = "" + number; // convert number input to string
// split input value into LHS and RHS using decpoint as divider
var dec = number.indexOf(this.decpoint) != -1;
var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";
// split format string into LHS and RHS using decpoint as divider
dec = format.indexOf(this.decpoint) != -1;
var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";
// adjust decimal places by cropping or adding zeros to LHS of number
if (srightEnd.length < nrightEnd.length) {
var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
nrightEnd = nrightEnd.substring(0, srightEnd.length);
if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1); // round up
// patch provided by Patti Marcoux 1999/08/06
while (srightEnd.length > nrightEnd.length) {
nrightEnd = "0" + nrightEnd;
}
if (srightEnd.length < nrightEnd.length) {
nrightEnd = nrightEnd.substring(1);
nleftEnd = (nleftEnd - 0) + 1;
}
} else {
for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
if (srightEnd.charAt(i) == "0") nrightEnd += "0"; // append zero to RHS of number
else break;
}
}
// adjust leading zeros
sleftEnd = this.strip(sleftEnd, "#"); // remove hashes from LHS of format
while (sleftEnd.length > nleftEnd.length) {
nleftEnd = "0" + nleftEnd; // prepend zero to LHS of number
}
if (useSeparator) nleftEnd = this.separate(nleftEnd, this.separator); // add separator
var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : ""); // combine parts
output = ((useCurrency) ? this.currency : "") + output + ((usePercent) ? this.percent : "");
if (isNegative) {
// patch suggested by Tom Denn 25/4/2001
output = (useCurrency) ? "(" + output + ")" : "-" + output;
}
return output;
},
// Original JavaScript code of this function by Duncan Crombie: dcrombie@chirp.com.au
strip : function(input, chars) { // strip all characters in 'chars' from input
var output = ""; // initialise output string
for (var i=0; i < input.length; i++)
if (chars.indexOf(input.charAt(i)) == -1)
output += input.charAt(i);
return output;
},
// Original JavaScript code of this function by Duncan Crombie: dcrombie@chirp.com.au
separate : function(input, separator) { // format input using 'separator' to mark 000's
input = "" + input;
var output = ""; // initialise output string
for (var i=0; i < input.length; i++) {
if (i != 0 && (input.length - i) % 3 == 0) output += separator;
output += input.charAt(i);
}
return output;
}
};